let cityGeojson = {/* 原有geojson数据保持不变 */};

let bboxList = [];

function geojson2bboxList(geojson, metre) {
    const coordinates = geojson.features[0].geometry.coordinates;
    let [minLon, minLat, maxLon, maxLat] = calculateBoundingBox(coordinates);
    
    // 计算纬度步长（固定值）
    const latStep = metre / 111319.9;
    
    // 遍历纬度带
    for (let lat = minLat; lat <= maxLat; lat += latStep) {
        // 动态计算经度步长（考虑纬度影响）
        const lonStep = metre / (111319.9 * Math.cos(lat * Math.PI / 180));
        
        // 遍历经度带
        for (let lon = minLon; lon <= maxLon; lon += lonStep) {
            const bbox = [
                parseFloat(lon.toFixed(6)),
                parseFloat(lat.toFixed(6)),
                parseFloat((lon + lonStep).toFixed(6)),
                parseFloat((lat + latStep).toFixed(6))
            ];
            
            // 使用四角检测法提高准确性
            if (isGridIntersects(bbox, coordinates)) {
                bboxList.push(bbox);
            }
        }
    }
    
    // 处理国际日期变更线附近的情况
    return adjustCrossDateLine(bboxList);
}

// 计算地理包围盒
function calculateBoundingBox(coordinates) {
    let minLon = Infinity, minLat = Infinity, 
        maxLon = -Infinity, maxLat = -Infinity;
    
    coordinates.forEach(polygon => {
        polygon[0].forEach(coord => {
            const [lon, lat] = coord;
            minLon = Math.min(minLon, lon);
            minLat = Math.min(minLat, lat);
            maxLon = Math.max(maxLon, lon);
            maxLat = Math.max(maxLat, lat);
        });
    });
    
    return [minLon, minLat, maxLon, maxLat];
}

// 改进的相交检测（检查网格四角）
function isGridIntersects(bbox, polygons) {
    const [west, south, east, north] = bbox;
    const testPoints = [
        [west, south],  // 西南角
        [east, south],  // 东南角
        [east, north],  // 东北角
        [west, north],  // 西北角
        [(west+east)/2, (south+north)/2]  // 中心点
    ];
    
    return testPoints.some(point => 
        isPointInPolygon(point, polygons)
    );
}

// 射线法判断点是否在多边形内
function isPointInPolygon(point, polygons) {
    let inside = false;
    const [x, y] = point;
    
    for (const polygon of polygons) {
        const ring = polygon[0];
        for (let i = 0, j = ring.length-1; i < ring.length; j = i++) {
            const [xi, yi] = ring[i];
            const [xj, yj] = ring[j];
            
            const intersect = ((yi > y) !== (yj > y)) &&
                (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
            if (intersect) inside = !inside;
        }
    }
    return inside;
}

// 处理国际日期变更线
function adjustCrossDateLine(bboxes) {
    return bboxes.map(bbox => {
        let [west, south, east, north] = bbox;
        if (east - west > 180) { // 跨越日期变更线
            return [east, south, west + 360, north];
        }
        return bbox;
    });
}

function _sleep(time = 500) {
    return new Promise((resolve, reject) => {
        const interval = setInterval(() => {
            try {
                if (isLoad() === true) {
                    clearInterval(interval);
                    resolve();
                }
            } catch (err) {
                clearInterval(interval);
                reject(err);
            }
        }, time);
        
        // 添加超时安全机制
        setTimeout(() => {
            clearInterval(interval);
            reject(new Error('Timeout waiting for load'));
        }, 30000); // 30秒超时
    });
}

function _sleep(time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}